#include "stdafx.h"
#include "traytest.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define WM_TRAYNOTIFY WM_USER + 100

CTrayNotifyIcon g_TrayIcon1;
CTrayNotifyIcon g_TrayIcon2;
CTrayNotifyIcon g_TrayIcon3;

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND_RANGE(MenuID, MenuID + 100, OnCommandRangeFxn)
	//ON_COMMAND(ID_HAPPY, OnHappy)
	//ON_UPDATE_COMMAND_UI(ID_HAPPY, OnUpdateHappy)
	//ON_COMMAND(ID_SAD, OnSad)
	//ON_UPDATE_COMMAND_UI(ID_SAD, OnUpdateSad)
	//ON_COMMAND(ID_ANIMATED, OnAnimated)
	//ON_UPDATE_COMMAND_UI(ID_ANIMATED, OnUpdateAnimated)
	//ON_COMMAND(ID_HIDE, OnHide)
	//ON_UPDATE_COMMAND_UI(ID_HIDE, OnUpdateHide)
	//ON_COMMAND(ID_SHOW, OnShow)
	//ON_UPDATE_COMMAND_UI(ID_SHOW, OnUpdateShow)
	//ON_WM_TIMER()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_TRAYNOTIFY, OnTrayNotification)
END_MESSAGE_MAP()

CMainFrame::CMainFrame()
{
//	m_bHappy = TRUE;
	m_hIcons[0] = CTrayNotifyIcon::LoadIcon(IDR_HAPPY);
	// m_hIcons[1] = CTrayNotifyIcon::LoadIcon(IDR_SAD);
//	m_nTimerCount = 0;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	//Let the base class do its thing
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// if (!g_TrayIcon1.Create(this, IDR_TRAYPOPUP, _T("Static Tray Icon"), _T("Demo Tray Application"), _T("Static Tray Icon"), 10, CTrayNotifyIcon::User, m_hIcons[0], WM_TRAYNOTIFY, 0, TRUE, FALSE, FALSE, NULL)) //For testing purposes initally creating a tray icon hidden!
	if (!g_TrayIcon1.Create(this, IDR_TRAYPOPUP, _T("Static Tray Icon"), _T("Demo Tray Application"), _T("Static Tray Icon"), 10, CTrayNotifyIcon::User, m_hIcons[0], WM_TRAYNOTIFY, 0, TRUE, FALSE, FALSE, NULL)) //For testing purposes initally creating a tray icon hidden!
	{
		AfxMessageBox(_T("Failed to create tray icon 1"), MB_OK | MB_ICONSTOP);
		return -1;
	}
	//For testing purposes, change the default menu item on the first icon to be the bottom item on the context menu
	g_TrayIcon1.SetDefaultMenuItem(ID_APP_ABOUT, FALSE);

	return 0;
}

LRESULT CMainFrame::OnTrayNotification(WPARAM wParam, LPARAM lParam)
{
	//Delegate all the work back to the default implementation in
	//CTrayNotifyIcon.
	g_TrayIcon1.OnTrayNotification(wParam, lParam);
	return S_OK;
}

void CMainFrame::OnDestroy() 
{
	//Destroy our timer

	//Let the parent class do its thing
	CFrameWnd::OnDestroy();
}

void CMainFrame::OnCommandRangeFxn(UINT nId)
{
	int nPos = nId - MenuID;

	std::list<std::pair<HWND, std::pair<HICON, CString> > >::iterator iter = g_TrayIcon1.m_listTaskBarData.begin();
	for (int n = 1; n < nPos; ++n)
	{
		++iter;
	}
	
	if (::GetWindowLong(iter->first, GWL_STYLE)& WS_VISIBLE)
	{
		::ShowWindow(iter->first , SW_HIDE);
	}	
	else
	{
		::ShowWindow(iter->first , SW_SHOW);
		::ShowWindow(iter->first, SW_NORMAL);
		::SetForegroundWindow(iter->first);
	}
}
